package net.uiiang.android.alkaid.utils;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class BitmapUtil {
    public static Bitmap getBitmap(Context context, String photoUriPath)
            throws Exception {
        Uri photoUri = Uri.parse("file://" + photoUriPath);
        InputStream photoStream = context.getContentResolver().openInputStream(
                photoUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream, null,
                options);
        int h = photoBitmap.getHeight();
        int w = photoBitmap.getWidth();
        if ((w > h) && (w > 128)) {
            double ratio = 128d / w;
            w = 128;
            h = (int) (ratio * h);
        } else if ((h > w) && (h > 128)) {
            double ratio = 128d / h;
            h = 128;
            w = (int) (ratio * w);
        }
        Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
        photoBitmap.recycle();
        return scaled;
    }

}
